package com.ncmmall.mall.controller.business.subject;

import com.ncmmall.application.business.subject.SubjectActivityApplication;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.subject.SubjectActivity;
import com.ncmmall.domain.business.subject.SubjectActivityType;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专题管理
 *
 * @author likaikai
 * @date 2020-09-01 10:47:57
 */
@Controller
@RequestMapping("/mall/subjectActivity")
@Slf4j
public class SubjectActivityController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private SubjectActivityApplication subjectActivityApplication;

    /**
     * 专题列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageParams pageParams, Model model, SubjectActivity params) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());
        String hql = "select t from SubjectActivity t where 1 = 1";
        Map<String, Object> map = new HashMap<>();
        if (params.getStatus() > 0) {
            hql += " and t.status = :status";
            map.put("status", params.getStatus());
        }
        if (StringUtils.isNotBlank(params.getName())) {
            hql += " and t.name like :name";
            map.put("name", "%" + params.getName() + "%");
        }
        if (StringUtils.isNotBlank(params.getDescription())) {
            hql += " and t.description like :description";
            map.put("description", "%" + params.getDescription() + "%");
        }
        if (params.getType() != null && params.getType().getId() > 0) {
            hql += " and t.type.id = :typeId";
            map.put("typeId", params.getType().getId());
        }
        hql += " order by t.createTime desc";

        QueryPage<SubjectActivity> page = queryChannelService.findPage(hql, pageRequest.setParams(map), SubjectActivity.class);
        List<SubjectActivityType> types = queryChannelService.findAll("select t from SubjectActivityType t where t.status = 2", SubjectActivityType.class);
        model.addAttribute("types", types);
        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/subjectActivity/list";
    }

    /**
     * 添加专题页
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        List<SubjectActivityType> types = queryChannelService.findAll("select t from SubjectActivityType t where t.status = 2", SubjectActivityType.class);
        model.addAttribute("types", types);
        model.addAttribute("subjectActivity", new SubjectActivity());
        model.addAttribute("_method", "POST");
        return "mall/subjectActivity/form";
    }

    /**
     * 添加专题页
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(SubjectActivity params) {
        try {

            subjectActivityApplication.create(params);
            return ImmutableMap.of("status", true, "message", "添加成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[添加专题异常]", e);
            return ImmutableMap.of("status", false, "message", "添加异常");
        }
    }


    /**
     * 编辑专题页
     */
    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(Model model, long id) {
        List<SubjectActivityType> types = queryChannelService.findAll("select t from SubjectActivityType t where t.status = 2", SubjectActivityType.class);
        model.addAttribute("types", types);
        model.addAttribute("subjectActivity", queryChannelService.findOne(SubjectActivity.class, id));
        model.addAttribute("_method", "PUT");
        return "mall/subjectActivity/form";
    }

    /**
     * 编辑专题页
     */
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modify(SubjectActivity params) {
        try {
            SubjectActivity subjectActivity = queryChannelService.findOne(SubjectActivity.class, params.getId());
            subjectActivityApplication.modify(params, subjectActivity);
            return ImmutableMap.of("status", true, "message", "保存成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改专题异常]", e);
            return ImmutableMap.of("status", false, "message", "保存异常");
        }
    }



    /**
     * 启用专题
     */
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> enable(long id) {
        try {
            SubjectActivity subjectActivity = queryChannelService.findOne(SubjectActivity.class, id);
            Requires.notNull(subjectActivity, "专题不存在");

            subjectActivityApplication.enable(subjectActivity);
            return ImmutableMap.of("status", true, "message", "上架成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题上架异常]", e);
            return ImmutableMap.of("status", false, "message", "上架异常");
        }
    }

    /**
     * 禁用专题
     */
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> disable(long id) {
        try {
            SubjectActivity subjectActivity = queryChannelService.findOne(SubjectActivity.class, id);
            Requires.notNull(subjectActivity, "专题不存在");

            subjectActivityApplication.disable(subjectActivity);
            return ImmutableMap.of("status", true, "message", "下架成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题下架异常]", e);
            return ImmutableMap.of("status", false, "message", "下架异常");
        }
    }

    /**
     * 删除专题
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(long id) {
        try {
            SubjectActivity subjectActivity = queryChannelService.findOne(SubjectActivity.class, id);
            Requires.notNull(subjectActivity, "专题不存在");

            subjectActivityApplication.remove(subjectActivity);
            return ImmutableMap.of("status", true, "message", "删除成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[专题删除异常]", e);
            return ImmutableMap.of("status", false, "message", "删除异常");
        }
    }

}
